# app/forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, IntegerField, DateField, TextAreaField
from wtforms.validators import DataRequired, Email, EqualTo, Length, NumberRange, Optional

class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('记住我')
    submit = SubmitField('登录')

class RegisterForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(min=4, max=64)])
    email = StringField('邮箱', validators=[DataRequired(), Email()])
    password = PasswordField('密码', validators=[DataRequired(), Length(min=6)])
    password2 = PasswordField('确认密码', validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('注册')

class ProfileForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(min=4, max=64)])
    email = StringField('邮箱', validators=[DataRequired(), Email()])
    password = PasswordField('新密码（留空则不修改）', validators=[Optional(), Length(min=6)])
    password2 = PasswordField('确认新密码', validators=[Optional(), EqualTo('password')])
    submit = SubmitField('保存')

class StudentForm(FlaskForm):
    student_id = StringField('学号', validators=[DataRequired(), Length(max=20)])
    name = StringField('姓名', validators=[DataRequired(), Length(max=64)])
    gender = SelectField('性别', choices=[('男', '男'), ('女', '女'), ('其他', '其他')])
    birthdate = DateField('出生日期', format='%Y-%m-%d', validators=[Optional()])
    class_name = StringField('班级', validators=[DataRequired(), Length(max=50)])
    submit = SubmitField('提交')

class CourseForm(FlaskForm):
    course_id = StringField('课程编号', validators=[DataRequired(), Length(max=20)])
    name = StringField('课程名称', validators=[DataRequired(), Length(max=64)])
    credit = IntegerField('学分', validators=[DataRequired(), NumberRange(min=1, max=5)])
    teacher = StringField('授课教师', validators=[DataRequired(), Length(max=64)])
    submit = SubmitField('提交')

class ScoreForm(FlaskForm):
    student_id = SelectField('学生', coerce=int, validators=[DataRequired()])
    course_id = SelectField('课程', coerce=int, validators=[DataRequired()])
    score = IntegerField('成绩', validators=[DataRequired(), NumberRange(min=0, max=100)])
    remark = TextAreaField('备注', validators=[Optional(), Length(max=256)])
    submit = SubmitField('提交')